package com.tulele.vtool.handler;

import com.tulele.vtool.model.respEntity.RespData;
import com.tulele.vtool.model.respEntity.ReturnCode;
import com.tulele.vtool.utils.EhcacheUtil;
import com.tulele.vtool.utils.RedisCacheUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
@RequestMapping("/cache")
public class CacheHandler {

    @Autowired
    private RedisCacheUtil redisCacheUtil;

    @GetMapping(value = "/delCache", produces="application/json;charset=UTF-8")
    public RespData delCache(String key) {

        EhcacheUtil.getInstance().removeCacheNameAll(key);

        RespData respData = new RespData();
        respData.setCode(ReturnCode.NORMAL_SUCC);
        respData.setMsg("成功");
        return respData;
    }

    @GetMapping(value = "/getAllCacheInfo", produces="application/json;charset=UTF-8")
    public RespData getAllCacheInfo(String key) {

        String [] cacheNameList = EhcacheUtil.getInstance().getCacheName();

        RespData respData = new RespData();
        respData.setData(cacheNameList);
        respData.setCode(ReturnCode.NORMAL_SUCC);
        respData.setMsg("成功");
        return respData;
    }

    @GetMapping(value = "/delAllCacheInfo", produces="application/json;charset=UTF-8")
    public RespData delAllCacheInfo(String key) {

        EhcacheUtil.getInstance().removeAll();

        RespData respData = new RespData();
        respData.setCode(ReturnCode.NORMAL_SUCC);
        respData.setMsg("成功");
        return respData;
    }

    @GetMapping(value = "/delRedisCacheInfo", produces="application/json;charset=UTF-8")
    public RespData delRedisCacheInfo(String key) {

        redisCacheUtil.removeSingleCache(key);

        RespData respData = new RespData();
        respData.setCode(ReturnCode.NORMAL_SUCC);
        respData.setMsg("成功");
        return respData;
    }

}
